package com.server.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.server.service.ICommonDataService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;

@Controller
@RequestMapping("common")
public class CommonDataController {
	
	@Autowired
	private ICommonDataService commonDataService;

	@RequestMapping("country")
	public void country(HttpServletResponse response){
		List<Map<String, Object>> countryData = commonDataService.countryData();
		OutputUtil.returnString(response, JSONSerielizer.toJson(countryData, null, null));
	}
	@RequestMapping("continent")
	public void continent(HttpServletResponse response,HttpServletRequest request){
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name)){
			Map<String, Object> countryData = commonDataService.continent(name);
			OutputUtil.returnString(response, JSONSerielizer.toJson(countryData, null, null));
		}else{
			OutputUtil.returnString(response, JSONSerielizer.toJson(null, null, null));
		}
		
	}
	@RequestMapping("class")
	public void classData(HttpServletResponse response){
		List<Map<String, Object>> classData = commonDataService.classData();
		OutputUtil.returnString(response, JSONSerielizer.toJson(classData, null, null));
	}
	@RequestMapping("users")
	public void users(HttpServletRequest request,HttpServletResponse response){
		String table = "education.user";
		String _whereStr = "valid=1";
		String sortStr = "nickName asc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(table, _whereStr, sortStr, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@RequestMapping("mngUsers")
	public void mngUsers(HttpServletRequest request,HttpServletResponse response){
		String table = "education.admin_user";
		String _whereStr = "valid=1";
		String sortStr = "nickName asc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(table, _whereStr, sortStr, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@RequestMapping("province")
	public void province(HttpServletRequest request,HttpServletResponse response){
		List<Map<String, Object>> provinceData = commonDataService.provinceData();
		OutputUtil.returnString(response, JSONSerielizer.toJson(provinceData, null, null));
	}
	@RequestMapping("city")
	public void city(HttpServletRequest request,HttpServletResponse response){
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name)){
			List<Map<String, Object>> provinceData = commonDataService.cityData(name);
			OutputUtil.returnString(response, JSONSerielizer.toJson(provinceData, null, null));
		}else{
			OutputUtil.returnString(response, JSONSerielizer.toJson(null, null, null));
		}
		
	}
}
